package com.nekoneko.nekonekodemo.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;


/**
 * Created by 想法的猫 on 2017/7/5 0005.
 */

public class CircleProcessView extends FrameLayout {
    float process = 0;
    Paint paint = new Paint();
    RectF rectF = new RectF();
    float paintWidth = 1.0f;
    double r = 360f / 100f;
    int color = Color.WHITE;
    Path path = new Path();

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        paint.setColor(color);
    }

    public CircleProcessView(Context context) {
        super(context);
        init();
    }

    public void updateProcess(float process) {
        if (process == 100) {
            process = 0;
        }
        if (this.process == process) {
            return;
        }
        this.process = process;
        postInvalidate();
    }

    public CircleProcessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleProcessView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public float getPaintWidth() {
        return paintWidth;
    }

    public void setPaintWidth(float paintWidth) {
        this.paintWidth = paintWidth;
        paint.setStrokeWidth(paintWidth);
        requestLayout();
    }


    private void init() {
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(paintWidth);
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setDither(true);
        setWillNotDraw(false);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        rectF.left = paintWidth;
        rectF.right = r - l - paintWidth;
        rectF.top = paintWidth;
        rectF.bottom = b - t - paintWidth;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        path.addArc(rectF, 0, (float) (process * r));
        canvas.drawPath(path, paint);
    }
}
